package com.guang.syncronized.review_day1;

import org.openjdk.jol.info.ClassLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author liguang
 * @Date 2022/10/22/18:26
 */
public class SyncronziedWaitTest {
    private final static Logger logger = LoggerFactory.getLogger(SyncronziedWaitTest.class);
    static volatile boolean flag = false;
    public static void main(String[] args) throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        Object object = new Object();
        logger.info("无锁可偏向，101。打印对象头：{}", ClassLayout.parseInstance(object).toPrintable());
        new Thread(()->{
            synchronized (object){
                while (!flag){
                    try {
                        object.wait();
                        logger.info("被唤醒，当前的flag标记是：{}",flag);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                logger.info("退出同步代码块，当前对象头是010。打印对象头：{}",ClassLayout.parseInstance(object).toPrintable());
            }
        },"t1").start();
        TimeUnit.SECONDS.sleep(5);
        synchronized (object){
            flag = true;
            object.notify();
        }

    }
}
